package com.cmj.service.impl;

import com.cmj.Utils.BeanCopyUtils;
import com.cmj.Utils.SetCbytimeUbytime;
import com.cmj.dao.CommentMapper;
import com.cmj.dao.UserMapper;
import com.cmj.domain.AppHttpCodeEnum;
import com.cmj.domain.ResponseResult;
import com.cmj.domain.entity.Comment;
import com.cmj.domain.vo.CommentVo;
import com.cmj.domain.vo.PageVo;
import com.cmj.exception.SystemException;
import com.cmj.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-03-14 16:56:26
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    /***
     * 文章评论
     * @param commentType 评论类型
     * @param articleId 文章id
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return 操作结果
     */
    @Override
    public ResponseResult commentList(int commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //根据文章id查询所有根评论
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> commentList = commentMapper.commentList(commentType,articleId);

        List<CommentVo> commentVos = toCommentVoList(commentList);

        //子评论的实现
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo : commentVos) {
            //查询对应的子评论
            List<Comment> children = commentMapper.commentListChildren(commentVo.getId());
            //赋值
            List<CommentVo> childrenVo = toCommentVoList(children);
            commentVo.setChildren(childrenVo);
        }
        //封装vo返回
        PageVo pageVo = new PageVo(commentVos, new PageInfo(commentList).getTotal());
        return ResponseResult.okResult(pageVo);
    }

    //封装到vo 并查询用户昵称
    private List<CommentVo> toCommentVoList(List<Comment> list) {
        //封装到vo
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //设置用户评论昵称
        for (CommentVo commentVo : commentVos) {
            //用户评论昵称
            String nickNameById = userMapper.getNickNameById(commentVo.getCreateBy());
            commentVo.setUsername(nickNameById);
            //设置头像
            String avatar = userMapper.getAvatar(commentVo.getCreateBy());
            commentVo.setAvatar(avatar);

            if (commentVo.getToCommentUserId() != -1){
                //跟评论昵称
                String ToCommentUser = userMapper.getNickNameById(commentVo.getToCommentUserId());
                commentVo.setToCommentUserName(ToCommentUser);
                //跟评头像
                String avatar1 = userMapper.getAvatar(commentVo.getCreateBy());
                commentVo.setAvatar(avatar1);
            }
        }
        return commentVos;
    }

    /***
     * 评论添加
     * @param comment 评论信息
     * @return 操作结果
     */
    @Transactional
    @Override
    public ResponseResult addComment(Comment comment) {
        //判断评论是否为空
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        //添加评论
        SetCbytimeUbytime.setCreate(comment);
        commentMapper.addComment(comment);

        //返回操作结果
        return ResponseResult.okResult();
    }
}
